package com.zcr.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcr.pojo.Druginfo;
import com.zcr.pojo.Gonggao;
import com.zcr.pojo.Jinhuorecords;
import com.zcr.pojo.Manager;
import com.zcr.service.GonggaoService;
import com.zcr.vo.Result;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.util.List;

/**
 * @version 1.0
 * @author： 张诚
 * @date： 2021-03-13 12:17
 */
@Controller
@RequestMapping("/gonggao")
public class GonggaoController {

    @Resource
    private GonggaoService gonggaoService;

    @GetMapping("/index")
    public String login(){
        return "shouye";
    }

    @ResponseBody()
    @PostMapping("/add")
    public Result add(String totalname,String neirong,HttpSession httpSession){
        Manager manager= (Manager) httpSession.getAttribute("LoginUser");
        System.out.println(manager.getId());
        System.out.println(manager.getUser());
        System.out.println(totalname);
        System.out.println(neirong);
        Gonggao gonggao=new Gonggao();
        gonggao.setBiaoti(totalname);
        gonggao.setNeirong(neirong);
        gonggao.setShijian(LocalDate.now());
        gonggao.setManagerid(manager.getId());
        gonggao.setManager(manager.getUser());
        return Result.sucess(gonggaoService.addGongGao(gonggao));
    }

    @ResponseBody()
    @PostMapping("/delete")
    public Result deleteAllDruginfos(@RequestBody String[] ids){
        for (int i=0;i<ids.length;i++){
            System.out.println(ids[i]);
        }
        try {
            gonggaoService.deleteAllDruginfo(ids);
            return Result.success("删除公告成功");
        }catch (Exception ex){
            return Result.fail("删除药品失败",ex);
        }
    }

    @ResponseBody()
    @GetMapping("/gonggaoindex")
    public Result getbyjinhuojiru(String drugname){
        List<Gonggao> list =gonggaoService.getByGongGao(drugname);
        return Result.sucess(list);
    }

    @ResponseBody()
    @PostMapping("/update")
    public Result updareDruginfo(Gonggao gonggao){
        try {
            gonggaoService.update(gonggao);
            return Result.success("修改公告成功");
        }catch (Exception ex){
            return Result.fail("修改公告失败",ex);
        }
    }

}
